package org.apache.rocketmq.console.config;

import org.apache.rocketmq.console.shiro.entity.UumsRequestPam;
import org.apache.rocketmq.console.shiro.entity.UumsResResult;
import org.apache.rocketmq.console.util.HttpClientUtil;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import com.alibaba.fastjson.JSON;

@Configuration
@ConfigurationProperties(prefix = "shiro.config")
public class LoginConfigure {
	private String uumsGetUserByLoginPassUrl;

	public String getUumsGetUserByLoginPassUrl() {
		return uumsGetUserByLoginPassUrl;
	}

	public void setUumsGetUserByLoginPassUrl(String uumsGetUserByLoginPassUrl) {
		this.uumsGetUserByLoginPassUrl = uumsGetUserByLoginPassUrl;
	}

	public UumsResResult loginByUUMS(UsernamePasswordToken token) {
		UumsRequestPam uumsRequestPam = new UumsRequestPam();
		uumsRequestPam.setLoginName(token.getUsername());
		uumsRequestPam.setPassword(String.valueOf(token.getPassword()));
		uumsRequestPam.setSysName("DPRMQ");
		String parm = JSON.toJSONString(uumsRequestPam);
		String uumsResResultJSON = HttpClientUtil.sendHttpPost(uumsGetUserByLoginPassUrl, parm);
		UumsResResult uumsResResult = JSON.parseObject(uumsResResultJSON, UumsResResult.class);
		if (!"200".equals(uumsResResult.getStatusCode())) {
			return new UumsResResult();
		}
		
		return uumsResResult;
	}
}
